// __PUBLIC__/js/page/weiba/admin/postlist
require(['../../common'], function() {
  require(['admin.postlist'])
})

define('admin.postlist', [
  'jquery',
  'page/grid',
], function($, Grid) {
  'use strict';

  var $table = $('.table')
  var mygrid = new Grid($table)
  mygrid.init();


  function handleSelected(callback) {
    var selected = mygrid.getSelected()
    if (!selected.count) {
      alert('请选择一条数据')
    } else {
      if (confirm('确定操作选中内容？')) {
        callback(selected)
      }
    }
  }

  /**
   * 处理ajax返回数据之后的刷新操作
   */

  function ajaxReload(obj, callback) {
    if ("undefined" == typeof(callback)) {
      callback = "location.href = location.href";
    } else {
      callback = 'eval(' + callback + ')';
    }
    if (obj.status == 1) {
      alert(obj.data)
      setTimeout(callback, 1500);
    } else {
      alert(obj.data)
    }
  };

  /**
   * 设置帖子状态
   * @param integer post_id 帖子ID
   * @param integer type 要设置的帖子类型 1:推荐，2:精华，3:置顶
   * @param integer curValue 当前状态值
   * @param integer topValue 置顶值，仅置顶用到
   * @return void
   */

  function setPost(selected) {
    $.post(APP + '?app=weiba&m=Admin&a=setPost', {
      post_id: selected.id.join(),
      type: selected.post1,
      curValue: selected.top,
      topValue: selected.post2
    }, function(msg) {
      ajaxReload(msg);
    }, 'json');
  };

  /**
   * 删除帖子至回收站
   * @param integer post_id 帖子ID
   * @return void
   */

  function delPost(selected) {
    $.post(APP + '?app=weiba&m=Admin&a=delPost', {
      post_id: selected.id.join()
    }).done(function(msg) {
      var msg = JSON.parse(msg)
      if (msg.status === 1) {
        mygrid.del(selected.index)
      }
    })
  }

  /**
   * 调整帖子评论楼层
   * @param integer post_id 帖子ID
   * @return void
   */

  function doStorey(selected) {
    $.post(APP + '?app=weiba&m=Admin&a=doStorey', {
      post_id: selected.id.join()
    }).done(function(msg) {
      if (msg === '1') {
        alert('操作成功')
      }
    })
  }

  /**
   * 还原已删除的帖子
   * @param mixed post_id 帖子ID
   * @return void
   */

  function recoverPost(selected) {
    $.post(APP + '?app=weiba&m=Admin&a=recoverPost', {
      post_id: selected.id.join()
    }, function(msg) {
      ajaxReload(msg);
    }, 'json');
  }

  /**
   * 删除帖子至回收站
   * @param integer post_id 帖子ID
   * @return void
   */

  function deletePost(selected) {
    $.post(APP + '?app=weiba&m=Admin&a=deletePost', {
      post_id: selected.id.join()
    }, function(msg) {
      ajaxReload(msg);
    }, 'json');
  }

  $(document)
    .on('click', '#J_storey', function(e) {
      e.preventDefault()
      handleSelected(function(selected) {
        doStorey(selected)
      })
    })
    .on('click', '#J_del', function(e) {
      e.preventDefault()
      handleSelected(function(selected) {
        deletePost(selected)
      })
    })
    .on('click', '#J_post', function(e) {
      e.preventDefault()
      handleSelected(function(selected) {
        delPost(selected)
      })
    })
    .on('click', '.A_del', function(e) {
      e.preventDefault()
      if (confirm('确定要删除选中内容？')) {
        var current = mygrid.getCurrent(this)
        var selected = {
          id: [current.id],
          index: [current.index]
        }
        deletePost(selected)
      }
    })
    .on('click', '.A_storey', function(e) {
      e.preventDefault()
      var current = mygrid.getCurrent(this)
      var selected = {
        id: [current.id],
        index: [current.index]
      }
      doStorey(selected)
    })
    .on('click', '.A_localtop', function(e) {
      e.preventDefault()
      var current = mygrid.getCurrent(this)
      var top = $(this).data('top')
      var selected = {
        id: [current.id],
        index: [current.index],
        post1: 3,
        post2: 1,
        top: top
      }
      setPost(selected)
    })
    .on('click', '.A_globaltop', function(e) {
      e.preventDefault()
      var current = mygrid.getCurrent(this)
      var top = $(this).data('top')
      var selected = {
        id: [current.id],
        index: [current.index],
        post1: 3,
        post2: 2,
        top: top
      }
      setPost(selected)
    })
    .on('click', '.A_digest', function(e) {
      e.preventDefault()
      var current = mygrid.getCurrent(this)
      var top = $(this).data('digest')
      var selected = {
        id: [current.id],
        index: [current.index],
        post1: 2,
        top: top
      }
      setPost(selected)
    })
    .on('click', '.A_recommend', function(e) {
      e.preventDefault()
      var current = mygrid.getCurrent(this)
      var top = $(this).data('recommend')
      var selected = {
        id: [current.id],
        index: [current.index],
        post1: 1,
        top: top
      }
      setPost(selected)
    })
    .on('click', '.A_recoverpost', function(e) {
      e.preventDefault()
      var current = mygrid.getCurrent(this)
      var selected = {
        id: [current.id],
        index: [current.index]
      }
      recoverPost(selected)
    })
    .on('click', '#J_recover', function(e) {
      e.preventDefault()
      handleSelected(function(selected) {
        recoverPost(selected)
      })
    })

})